package com.bwie.gddproject.login.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.gddproject.R;
import com.bwie.gddproject.base.BaseActivity;
import com.bwie.gddproject.login.model.LoginBean;
import com.bwie.gddproject.login.precenter.LoginPresenter;
import com.bwie.gddproject.regist.view.RegistActivity;

import org.greenrobot.eventbus.EventBus;

public class LoginActivity extends BaseActivity implements ILoginActivity, View.OnClickListener {

    private ImageView mBack;
    /**
     * 手机号
     */
    private EditText mEtPhone;
    /**
     * 请输入密码
     */
    private EditText mEtPwd;
    private ImageView mIvPwd;
    /**
     * 登陆
     */
    private Button mBtLogin;
    /**
     * 请注册
     */
    private TextView mTvRegister;
    private LinearLayout mActivityLogin;
    private LoginPresenter loginPresenter;
    private ImageView mQqlogin;
    private ImageView mWeixinlogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        SharedPreferences sharedPreferences = getSharedPreferences("user",
                Activity.MODE_PRIVATE);
        String name = sharedPreferences.getString("name", "");
        String pwd = sharedPreferences.getString("pwd", "");
        mEtPhone.setText(name);
        mEtPwd.setText(pwd);


    }


    @Override
    public String getAccount() {
        return mEtPhone.getText().toString().trim();
    }

    @Override
    public String getPwd() {
        return mEtPwd.getText().toString().trim();
    }

    @Override
    public void show(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void toRegisterAc() {
        Intent intent = new Intent(this, RegistActivity.class);
        startActivityForResult(intent,1);
    }

    @Override
    public void toIndexAc(LoginBean loginBean) {
        EventBus.getDefault().post(loginBean);
        finishThis();
    }

    @Override
    public void finishThis() {
        this.finish();
    }

    @Override
    public void saveByShared() {
        SharedPreferences preferences=getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        editor.putString("name", getAccount());
        editor.putString("pwd", getPwd());
        editor.commit();
    }

    private void initView() {
        mBack = (ImageView) findViewById(R.id.back);
        mBack.setOnClickListener(this);
        mEtPhone = (EditText) findViewById(R.id.et_phone);
        mEtPwd = (EditText) findViewById(R.id.et_pwd);
        mIvPwd = (ImageView) findViewById(R.id.iv_pwd);
        mIvPwd.setOnClickListener(this);
        mBtLogin = (Button) findViewById(R.id.bt_login);
        mBtLogin.setOnClickListener(this);
        mTvRegister = (TextView) findViewById(R.id.tv_register);
        mTvRegister.setOnClickListener(this);
        mActivityLogin = (LinearLayout) findViewById(R.id.activity_login);
        loginPresenter = new LoginPresenter(this);
        mQqlogin = (ImageView) findViewById(R.id.qqlogin);
        mQqlogin.setOnClickListener(this);
        mEtPhone.setOnClickListener(this);
        mEtPwd.setOnClickListener(this);
        mWeixinlogin = (ImageView) findViewById(R.id.weixinlogin);
        mActivityLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.back:
                this.finish();
                break;
            case R.id.iv_pwd:
                break;
            case R.id.bt_login:
                loginPresenter.login();
                break;
            case R.id.tv_register:
                toRegisterAc();
                break;
            case R.id.qqlogin:
                show("第三方登录,还没完成!");
                break;
            case R.id.et_phone:
                break;
            case R.id.et_pwd:
                break;
            case R.id.activity_login:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode ==2){
            String mobile = data.getStringExtra("mobile");
            String password = data.getStringExtra("password");
            Log.e("tag",mobile+"ssss"+password);
            mEtPhone.setText(mobile);
            mEtPwd.setText(password);
        }
    }
}
